package com.woniuxy.mq.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 普通队列： normal.queue
 * 普通交换机： normal.exchange
 * 队列绑定到交换机： city.gz
 * <p>
 * 死信队列： dlx.queue
 * 死信交换机： dlx.exchange
 * 死信队列绑定到死信交换机： dlx.routing.key
 */
@Configuration
public class RabbitConfig05 {

    //创建普通队列 绑定 死信交换机 死信路由
    @Bean
    public Queue normalQueue() {
        return QueueBuilder.durable("normal.queue")  //普通队列名字
                .deadLetterExchange("dlx.exchange")    //死信交换机
                .deadLetterRoutingKey("dlx.routing.key")  //死信路由键
                .ttl(30000)   //30s 后 消息进 死信队列 （普通消费者 停止）
                .build();
    }

    //创建交换机
    @Bean
    public DirectExchange normalExchange() {
        return new DirectExchange("normal.exchange");
    }

    //队列绑定到交换机
    @Bean
    public Binding confirmToDirectExchange(Queue normalQueue, DirectExchange normalExchange) {
        return BindingBuilder.bind(normalQueue).to(normalExchange).with("city.gz");
    }

    @Bean
    public Queue dlxQueue() {
        return new Queue("dlx.queue");
    }

    @Bean
    public DirectExchange dxlExchange() {
        return new DirectExchange("dlx.exchange");
    }

    @Bean
    public Binding dlxToDirectExchange(Queue dlxQueue, DirectExchange dxlExchange) {
        return BindingBuilder.bind(dlxQueue).to(dxlExchange).with("dlx.routing.key");
    }
}